<template>
  <div class="container">
    <el-card>
      <!-- 表单 -->
      <el-row>
        <el-col :span="22">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="学科名称">
              <el-input v-model="requestParams.subjectName" size="small" placeholder="" />
            </el-form-item>
            <el-form-item>
              <el-button plain size="small" @click="clear()">清除</el-button>
              <el-button type="primary" size="small" @click="filterSubject()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2" style="text-align:right">
          <el-button size="small" icon="el-icon-edit" type="success" @click="addSubjects()">新增学科</el-button>
        </el-col>
      </el-row>
      <!-- 提示框 -->
      <el-alert :closable="false" :title="`数据一共有${total}条`" size="small" type="info" show-icon />
      <!-- 表格 -->
      <el-table v-loading="loading" class="tableBox" :data="subjectsList" style="width: 100%">
        <el-table-column type="index" :index="indexMethod" label="序号" width="50" />
        <el-table-column prop="subjectName" label="学科名称" width="100" />
        <el-table-column prop="username" label="创建者" width="150" />
        <el-table-column label="创建日期" width="200">
          <template #default="{row}">
            {{ row.addDate | parseTimeByString }}
          </template>
        </el-table-column>

        <el-table-column label="前台是否显示" width="160">
          <template #default="{row}">
            {{ row.isFrontDisplay===1 ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录" />
        <el-table-column prop="tags" label="标签" />
        <el-table-column prop="totals" label="题目数量" />
        <el-table-column label="操作" width="228">
          <template #default="{row}">
            <el-button
              type="text"
              size="small"
              @click="$router.push({path:'/subjects/directorys',query:{id:row.id,name:row.subjectName}})"
            >
              学科分类</el-button>
            <el-button
              type="text"
              size="small"
              @click="$router.push({path:'/subjects/tags',query:{id:row.id,name:row.subjectName}})"
            >学科标签</el-button>
            <el-button type="text" size="small" @click="editSubjects(row)">修改</el-button>
            <el-button type="text" size="small" @click="delSubject(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="text-align:right"
        background
        :current-page="requestParams.page"
        :page-sizes="[5, 10, 15, 30, 40]"
        :page-size="requestParams.pagesize"
        layout="prev, pager, next, sizes,jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <!-- 新增学科组件 -->
      <subjects-add
        ref="openDialog"
        :edit-data="editData"
        :show-subject-dialog.sync="showSubjectDialog"
        @reload="getSubjectsList()"
      />
    </el-card>
  </div>
</template>

<script>
import { list as reqGetSubjectsList, remove } from '@/api/hmmm/subjects' // detail
import { parseTimeByString } from '../../filters/index'
import SubjectsAdd from '../components/subjects-add.vue'
export default {
  name: 'Subjects',
  components: { SubjectsAdd },
  data() {
    return {
      // 表单 + 分页数据
      requestParams: {
        subjectName: '',
        page: 1,
        pagesize: 10
      },
      parseTimeByString,
      // 表格数据
      subjectsList: [],
      total: 0,
      loading: false,
      // 新增学科
      showSubjectDialog: false,
      editData: {}

    }
  },
  created() {
    this.getSubjectsList()
  },
  methods: {
    // 修改学科
    async editSubjects(row) {
      this.showSubjectDialog = true
      // const { data } = await detail(row)
      this.editData = row
      // 回显
      this.$nextTick(() => {
        this.$refs.openDialog.title = '修改学科'
        this.$refs.openDialog.open()
      })
    },

    // 新增学科
    addSubjects() {
      this.showSubjectDialog = true
      this.$nextTick(() => {
        this.$refs.openDialog.title = '新增学科'
      })
    },

    // 删除学科
    delSubject(row) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await remove(row)
        this.$message.success('删除成功')
        // 处理小bug
        if (this.subjectsList.length === 1 && this.requestParams.page > 1) {
          this.requestParams.page--
        }
        this.getSubjectsList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 表单搜索
    async filterSubject() {
      this.requestParams.page = 1
      this.getSubjectsList()
    },
    // 表单清除
    clear() {
      this.requestParams.subjectName = ''
      this.getSubjectsList()
    },
    // 列表渲染
    async getSubjectsList() {
      this.loading = true
      const { data: { items, counts }} = await reqGetSubjectsList(this.requestParams)
      this.subjectsList = items
      this.total = counts
      this.loading = false
    },
    // 分页相关
    handleSizeChange(pageNum) {
      this.requestParams.pagesize = pageNum
      this.requestParams.page = 1
      this.getSubjectsList()
    },
    handleCurrentChange(newPage) {
      this.requestParams.page = newPage
      this.getSubjectsList()
    },
    indexMethod(index) {
      return index + 1 + (this.requestParams.page - 1) * this.requestParams.pagesize
    }

  }
}
</script>

<style scoped lang='scss'>
.tableBox{
  margin-top: 10px;
}
</style>
